package me.zhengjie.modules.sync.processing;

import com.alibaba.fastjson.JSON;
import com.qiaofang.applet.common.constant.SyncTypeEnum;
import com.qiaofang.applet.common.util.RedisKeyUtil;
import com.qiaofang.applet.common.util.RedisUtil;
import com.qiaofang.applet.service.property.domain.EstatePhoto;
import com.qiaofang.applet.service.property.domain.SyncDataQueue;
import com.qiaofang.applet.service.property.service.EstatePhotoService;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.sync.ProcessingBase;
import me.zhengjie.utils.ImageUtil;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/*
 * 楼盘数据处理
 * */
@Slf4j
@Service
public class EstatePhotoSyncProcessing extends ProcessingBase {

    public EstatePhotoSyncProcessing() {
        setDataType(SyncTypeEnum.ESTATE_PHOTO.getSyncType());
    }

    @Autowired
    EstatePhotoService estatePhotoService;

    @Override
    protected void processing(SyncDataQueue syncDataQueue) {
        String batchContent = syncDataQueue.getBatchContent();
        List<EstatePhoto> list = JSON.parseArray(batchContent, EstatePhoto.class);
        if (null == list) {
            log.info("processing EstatePhoto is null");
            return;
        }
        list.stream().forEach(item -> {
            saveEstatePhoto(item, syncDataQueue);
        });
    }

    private void saveEstatePhoto(EstatePhoto item, SyncDataQueue syncDataQueue) {
        if (StringUtils.isEmpty(item.getEstateUuid())) {
            return;
        }
        String estateUniqueUuid = syncDataQueue.getCompanyUuid() + "_" + item.getEstateUuid();
        String photoUuid = item.getPhotoUuid();
        EstatePhoto estatePhoto = estatePhotoService.findByEstateAndPhotoUuid(estateUniqueUuid, photoUuid);
        if (!Objects.isNull(item.getDeleted()) && item.getDeleted()) {
            if(!Objects.isNull(estatePhoto)) {
                estatePhotoService.deleteById(estatePhoto.getId());
            }
            return;
        }
        if (Objects.isNull(estatePhoto)) {
            estatePhoto = new EstatePhoto();
        }
        //复制属性
        String[] ignoreProperties = new String[]{"id", "createBy", "updateBy", "createTime", "updateTime"};
        BeanUtils.copyProperties(item, estatePhoto, ignoreProperties);
        estatePhoto.setEstateUniqueUuid(estateUniqueUuid);
        estatePhoto.setCompanyUuid(syncDataQueue.getCompanyUuid());
        estatePhoto.setCityCode(syncDataQueue.getCityCode());
        estatePhoto.setCorporationCode(syncDataQueue.getCorporationCode());
        estatePhoto.setUpdateTime(new Date());
        estatePhoto.setLastSyncDate(new Date(System.currentTimeMillis() - DateUtil.DAY_MILLISECONDS));
        replaceDomain(estatePhoto);
        try {
            estatePhotoService.save(estatePhoto);
        } catch (Exception e) {
            log.error("saveEstatePhoto error ", e);
            //记录错误日志
            this.saveErrorLog(JSON.toJSONString(estatePhoto), e.getMessage(), syncDataQueue);
        }
    }

    //替换域名
    private void replaceDomain(EstatePhoto estatePhoto) {
        String photoURL = estatePhoto.getPhotoURL();
        if (StringUtils.isEmpty(photoURL)) {
            return;
        }
        if ((photoURL.startsWith(ImageUtil.HTTP) || photoURL.startsWith(ImageUtil.HTTPS)) && photoURL.contains(ImageUtil.DISCARD_IMAGE_URL)) {
            photoURL = StringUtils.replace(estatePhoto.getPhotoURL(), ImageUtil.DISCARD_IMAGE_URL, ImageUtil.IMAGE_URL);
        } else {
            photoURL = ImageUtil.HTTP + ImageUtil.IMAGE_URL + "/" + photoURL;
        }
        estatePhoto.setPhotoURL(photoURL);
    }
}
